package grpcom.com.liveotv;

import com.solbox.sbhlsexam.*;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/*
 * seeking UI (for VOD streaming)
 */
public class SBSeekingBar extends View {

	// bitmap resources
	private Bitmap m_sb_bg_left = null;
	private Bitmap m_sb_bg_right = null;
	private Bitmap m_sb_bg = null;
	private Bitmap m_sb_fg_left = null;
	private Bitmap m_sb_fg = null;
	private Bitmap m_sb_dis_left = null;
	private Bitmap m_sb_dis = null;
	private Bitmap m_sb_btn = null;
	private Bitmap m_sb_btn_dis = null;
	
	private boolean m_vod = false;			// stream type flag
	
	private int m_last_pos = 0;				// current time position (seconds)
	private int m_seek_pos = 0;				// time position to seek (seconds)
	private int m_dur = 0;					// duration (seconds)
	private Rect m_thumb_pos;				// rectangle of thumb button 
	private boolean m_seeking = false;		// flag whether is under seeking or not
	private boolean m_thumb_drag = false;	// flag whether is under dragging thumb button or not.
	
	private int m_last_drag_x = 0;
	
	private SBHLSExamActivity m_activity = null;

	/*
	 * constructors
	 */
	public SBSeekingBar(Context context) {
		
		super(context);
		
		this.setBitmapResource();
	}
	
	public SBSeekingBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		this.setBitmapResource();
	}
	
	public SBSeekingBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		
		this.setBitmapResource();
	}

	/*
	 * set SBHLSExamActivity
	 * 
	 */
	public void setActivity(SBHLSExamActivity activity) {
		
		this.m_activity = activity;
	}
	
	/*
	 * set stream type and duration
	 * 
	 * @param vod		if true, VOD streaming. otherwise live streaming.
	 * @param dur		duration (seconds)
	 */
	public void setVOD(boolean vod, int dur) {
		
		this.m_vod = vod;
		this.m_dur = dur;
	}
	
	/*
	 * set time position
	 * 
	 * @param pos	time position (seconds)
	 */
	public void setPos(int pos) {
		
		this.m_last_pos = pos;
		
		this.invalidate();
	}
	
	/*
	 * load bitmap resources
	 */
	private void setBitmapResource() {
		
		this.m_sb_bg_left = ((BitmapDrawable) this.getResources().getDrawable(R.drawable.sb_bg_left)).getBitmap();
		this.m_sb_bg_right = ((BitmapDrawable) this.getResources().getDrawable(R.drawable.sb_bg_right)).getBitmap();
		this.m_sb_bg = ((BitmapDrawable) this.getResources().getDrawable(R.drawable.sb_bg)).getBitmap();
		this.m_sb_fg_left = ((BitmapDrawable) this.getResources().getDrawable(R.drawable.sb_fg_left)).getBitmap();
		this.m_sb_fg = ((BitmapDrawable) this.getResources().getDrawable(R.drawable.sb_fg)).getBitmap();
		this.m_sb_dis_left = ((BitmapDrawable) this.getResources().getDrawable(R.drawable.sb_dis_left)).getBitmap();
		this.m_sb_dis = ((BitmapDrawable) this.getResources().getDrawable(R.drawable.sb_dis)).getBitmap();
		this.m_sb_btn = ((BitmapDrawable) this.getResources().getDrawable(R.drawable.thumb)).getBitmap();
		this.m_sb_btn_dis = ((BitmapDrawable) this.getResources().getDrawable(R.drawable.thumb_dis)).getBitmap();
		
		this.m_thumb_pos = new Rect(0, 0, 0, 0);
	}
	
	/*
	 * set seeking status
	 * 
	 *  @param pos		time position to seek
	 *  @param seek		if true, means is under seeking. otherwise is not.
	 */
	public synchronized void setSeekPos(int pos, boolean seek) {
		
		if(pos >= 0) this.m_seek_pos = pos; 	// if -1, ignore
		this.m_seeking = seek;
	}
	
	/*
	 * check whether is under seeking or not
	 */
	public boolean underSeeking() {
		
		return this.m_seeking;
	}
	
	protected void onDraw (Canvas canvas) {

		Rect src, dst;
		Paint paint = new Paint();
		
		paint.setFilterBitmap(true);
		
		// draw seeking bar (panel background)
		src = new Rect(0, 0, this.m_sb_bg_left.getWidth(), this.m_sb_bg_left.getHeight());
		dst = new Rect(203, 21, 211, 49);
		
		canvas.drawBitmap(this.m_sb_bg_left, src, dst, paint);
		
		src.set(0, 0, this.m_sb_bg.getWidth(), this.m_sb_bg.getHeight());
		dst.set(211, 21, this.getWidth() - 147, 49);
		
		canvas.drawBitmap(this.m_sb_bg, src, dst, paint);
		
		src.set(0, 0, this.m_sb_bg_right.getWidth(), this.m_sb_bg_right.getHeight());
		dst.set(this.getWidth() - 147, 21, this.getWidth() - 141, 49);
		
		canvas.drawBitmap(this.m_sb_bg_right, src, dst, paint);
		////////////////////////////////////////////////////////
		
		if(this.m_vod && this.m_dur > 0) { // if VOD streaming, (if live streaming, ignore)
			
			int pos = 0;
			
			if(this.underSeeking()) {

				// draw left side bar(?) of thumb button
				pos = (this.getWidth() - 344) * this.m_seek_pos / this.m_dur;

				src.set(0, 0, this.m_sb_dis_left.getWidth(), this.m_sb_dis_left.getHeight());
				dst.set(204, 19, 204 + (pos > 8 ? 8 : pos), 51);

				canvas.drawBitmap(this.m_sb_dis_left, src, dst, paint);
			
				if(pos > 8) {
				
					src.set(0, 0, this.m_sb_dis.getWidth(), this.m_sb_dis.getHeight());
					dst.set(212, 19, 203 + pos, 51);
				
					canvas.drawBitmap(this.m_sb_dis, src, dst, paint);
				}
				
				// draw thumb button
				src.set(0, 0, this.m_sb_btn.getWidth(), m_sb_btn.getHeight());
				this.m_thumb_pos.set(183 + pos, 15, 223 + pos, 55);
				
				canvas.drawBitmap(this.m_sb_btn_dis, src, this.m_thumb_pos, paint);

			} else {
				
				// draw left side bar(?) of thumb button
				pos = (this.getWidth() - 344) * this.m_last_pos / this.m_dur;
				
				src.set(0, 0, this.m_sb_fg_left.getWidth(), this.m_sb_fg_left.getHeight());
				dst.set(201, 19, 201 + (pos > 8 ? 8 : pos), 51);

				canvas.drawBitmap(this.m_sb_fg_left, src, dst, paint);
			
				if(pos > 8) {
				
					src.set(0, 0, this.m_sb_fg.getWidth(), this.m_sb_fg.getHeight());
					dst.set(209, 19, 203 + pos, 51);
				
					canvas.drawBitmap(this.m_sb_fg, src, dst, paint);
				}
				
				// draw thumb button
				src.set(0, 0, this.m_sb_btn.getWidth(), m_sb_btn.getHeight());
				this.m_thumb_pos.set(183 + pos, 15, 223 + pos, 55);
				
				canvas.drawBitmap(this.m_sb_btn, src, this.m_thumb_pos, paint);
			}
			
		}
	}
	
	public boolean onTouchEvent (MotionEvent event) {
		
		int et = event.getAction();

		// if under seeking or live streaming, ignore
		if(this.m_activity.underSeeking() || !this.m_vod) {

			// for UI continues to be shown
			this.m_activity.showUI();
			return true;
		}
		
		if(et == MotionEvent.ACTION_DOWN) {
			
			if(event.getX() > this.m_thumb_pos.left && event.getX() < this.m_thumb_pos.right) { 
				//&& event.getY() > this.m_thumb_pos.top && event.getY() < this.m_thumb_pos.bottom) {
				
				int pos;

				// mark that under dragging (begin of dragging).
				this.m_thumb_drag = true;
				
				// get time position from position of button
				pos = (int) ((event.getX() - 203) * this.m_dur / (this.getWidth() - 344));
				
				if(pos > this.m_dur) pos = this.m_dur;
				else if(pos < 0) pos = 0;

				// set seeking status
				this.setSeekPos(pos, true);
				
				// update UI
				this.invalidate();
				
				// not seek yet, just update UI (text)				
				this.m_activity.seek(pos, false);
				
				this.m_last_drag_x = (int)event.getX();
				
			} else if(this.m_thumb_drag == false && event.getX() >= 203 && event.getX() < this.getWidth() - 141) {
				
				// if click seeking bar, not thumb button
				
				int pos;
				
				// get time position from position of event
				pos = (int) ((event.getX() - 203) * this.m_dur / (this.getWidth() - 344));
				
				if(pos > this.m_dur) pos = this.m_dur;
				else if(pos < 0) pos = 0;

				// update UI
				this.setSeekPos(pos, true);
				this.invalidate();
				
				// seek 
				this.m_activity.seek(pos, true);
			}
			
			this.m_activity.showUI();
			
		} else if(et == MotionEvent.ACTION_MOVE) {
			
			if(this.m_thumb_drag && this.m_last_drag_x != (int)event.getX()) {
			
				// if under dragging,
				
				int pos;
				
				// get time position from position of button
				pos = (int) ((event.getX() - 203) * this.m_dur / (this.getWidth() - 344));
				
				if(pos > this.m_dur) pos = this.m_dur;
				else if(pos < 0) pos = 0;
				
				// update UI
				this.setSeekPos(pos, true);
				this.invalidate();
				
				// not seek yet, just update UI (text)
				this.m_activity.seek(pos, false);
				
				this.m_last_drag_x = (int)event.getX();
			}
			
			this.m_activity.showUI();
			
		} else if(et == MotionEvent.ACTION_CANCEL || et == MotionEvent.ACTION_OUTSIDE) {

			if(this.m_thumb_drag) {
				
				// if under dragging, cancel
				
				this.setSeekPos(-1, false);
				this.invalidate();
				this.m_activity.seek(-1, true);
				
				this.m_thumb_drag = false;
				
			}
			
		} else if(et == MotionEvent.ACTION_UP) {
			
			if(this.m_thumb_drag) {
				
				int pos;
				
				// if under dragging, 
				
				// get time position from position of button
				pos = (int) ((event.getX() - 203) * this.m_dur / (this.getWidth() - 344));
				
				if(pos > this.m_dur) pos = this.m_dur;
				else if(pos < 0) pos = 0;
				
				// update UI
				this.setSeekPos(pos, true);
				this.invalidate();
				
				// seek
				this.m_activity.seek(pos, true);
				
				this.m_thumb_drag = false;				
			}
			
		}
		
		return true;
	}
}
